package com.kasoft.emq.handler;

import com.kasoft.emq.handler.processor.AbstractProcessor;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class Dispatcher {

	private static final int MAX_THREAD_NUM = 10;

	private static ExecutorService executorService = Executors.newFixedThreadPool(MAX_THREAD_NUM);

	@SneakyThrows
	public static void submit(UTF8Buffer utf8Buffer, Buffer buffer, Runnable ack) {
		Class<?> executorClass = HandlerMap.getClassByTopic(utf8Buffer.toString());
		if(executorClass == null){
			log.error("当前路径不存在:[{}]", utf8Buffer.toString());
			return;
		}
		AbstractProcessor processor = (AbstractProcessor) SpringContextHolder.getBean(executorClass);
		processor.setMsg(buffer.toString());
		processor.setAck(ack);
		executorService.submit(processor);
	}
}
